﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace psensor_array_tool
{
    class Program
    {
        static void file_generate(string array_path)
        {
            string out_file_path = string.Empty;
            Console.WriteLine("out_file_path={0}", array_path);
            out_file_path = array_path.Replace(".txt", "") + "coros.bin";

            FileStream file = new FileStream(array_path, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader reader = new StreamReader(file);

            FileStream file_out_out = new FileStream(out_file_path, FileMode.Create, FileAccess.ReadWrite);
            BinaryWriter writer = new BinaryWriter(file_out_out);

            while (true)
            {
                try
                {
                    string line = reader.ReadLine();
                    if (line.Length == 0)
                    {
                        break;
                    }
                    string[] objs = new string[10];
                    objs = line.Split(',');

                    for (int i = 0; i < objs.Count(); i++)
                    {
                        objs[i] = objs[i].Replace("0x", "");
                    }
                    Int16 addr = Convert.ToInt16(objs[0], 16);
                    Int32 addr_value = Convert.ToInt32(objs[1], 16);

                    writer.Write(addr);
                    writer.Write(addr_value);
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee.ToString());
                    file_out_out.Flush();
                    break;
                }
            }
            long output_file_len = file_out_out.Length;
            Console.WriteLine("file.Length={0}", output_file_len);
            writer.Close();
            if (output_file_len==0)
            {
                File.Delete(out_file_path);
            }
            else
            {
                

            }
            file.Close();
            return;
        }
        static void Main(string[] args)
        {
            string array_path = string.Empty;
            string out_file_path = string.Empty;
            array_path = ".\\hr31参数-V1.2.txt";

            string currentDirectory = Directory.GetCurrentDirectory();
            string []dir_files =  Directory.GetFiles(currentDirectory);
            for(int i=0;i< dir_files.Count();i++)
            {
                if(dir_files[i].Contains(".txt")==true && dir_files[i].Contains("readme")==false)
                {
                    Console.WriteLine("path={0}", dir_files[i]);
                    file_generate(dir_files[i]);
                }
            }


            //生成coros bin文件
            currentDirectory = Directory.GetCurrentDirectory();
            dir_files = Directory.GetFiles(currentDirectory);
            for (int i = 0; i < dir_files.Count(); i++)
            {
                if (dir_files[i].Contains(".bin") == true && dir_files[i].Contains("63")==false)
                {
                    Console.WriteLine("path={0}", dir_files[i]);

                    string absolutePath = Path.GetFullPath("./"); // 获取绝对路径
                    try
                    {
                        //Process p = Process.Start("F:\\WorkSpace\\psensor_array_tool\\psensor_array_tool\\psensor_array_tool\\bin\\Debug\tools\\package.exe");


                        //Process p = Process.Start(absolutePath + "/start.bat", dir_files[i]);
                        //p.WaitForExit();//本行代码不是必须，但是很关键，限制等待外部程序退出后才能往下执行

                        // 创建一个新的进程实例
                        Process process = new Process();

                        // 设置要启动的进程信息
                        process.StartInfo.FileName = absolutePath + "start.bat";
                        process.StartInfo.UseShellExecute = false; // 不使用操作系统的shell来启动进程
                        process.StartInfo.RedirectStandardOutput = true; // 重定向标准输出（以便捕获命令的输出）
                        process.StartInfo.CreateNoWindow = true; // 不创建新的命令行窗口
                        process.StartInfo.Arguments = dir_files[i];

                        // 启动进程
                        bool ret=    process.Start();
                        process.WaitForExit();
                        File.Delete(dir_files[i]);
                        break;

                    }
                    catch (Exception ee)
                    {
                        Console.WriteLine(ee.ToString());
                    }
                }
            }
            Console.WriteLine("finish");
            Console.ReadLine();
        }
    }
}
